package bmatch.tech.todo.service.impl;

import bmatch.tech.todo.dao.BmUserMapper;
import bmatch.tech.todo.model.Result;
import bmatch.tech.todo.model.UserStatus;
import bmatch.tech.todo.model.dataobject.BmUser;
import bmatch.tech.todo.service.UserService;
import bmatch.tech.todo.util.IDUtils;
import bmatch.tech.todo.util.PwdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program:todo
 * @author:LinJinhao
 * @create:2019-01-23 17:29
 **/
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private BmUserMapper bmUserMapper;

	@Override
	public Result register(BmUser user) {
		BmUser newUser = new BmUser();
		String slat = "bmatch";

		newUser.setId(IDUtils.get32UUID());
		newUser.setEmail(user.getEmail());
		newUser.setPwd(PwdUtils.md5Password(user.getPwd(),slat));
		newUser.setSalt(slat);
		newUser.setStatus(UserStatus.DISABLED.toString());
		newUser.setRegTime(new Date());
		newUser.setGmtModified(new Date());

		bmUserMapper.insert(newUser);

		return null;
	}

	@Override
	public boolean activate(String userId) {
		if (bmUserMapper.updateStatusById(userId,UserStatus.ENABLED.toString()) < 0) {
			return false;
		}
		return true;
	}
}
